/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hhx.web;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hhx.entity.HhxFinance;
import com.thinkgem.jeesite.modules.hhx.entity.HhxInvest;
import com.thinkgem.jeesite.modules.hhx.service.HhxInvestService;

/**
 * 单表生成Controller
 * @author zmy
 * @version 2019-04-16
 */
@Controller
@RequestMapping(value = "${adminPath}/hhx/hhxInvest")
public class HhxInvestController extends BaseController {

	@Autowired
	private HhxInvestService hhxInvestService;
	
	@ModelAttribute
	public HhxInvest get(@RequestParam(required=false) String id) {
		HhxInvest entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hhxInvestService.get(id);
		}
		if (entity == null){
			entity = new HhxInvest();
		}
		return entity;
	}
	
	@RequiresPermissions("hhx:hhxInvest:view")
	@RequestMapping(value = {"list", ""})
	public String list(HhxInvest hhxInvest, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HhxInvest> page = hhxInvestService.findPage(new Page<HhxInvest>(request, response), hhxInvest); 
		model.addAttribute("page", page);
		return "modules/hhx/hhxInvestList";
	}

	@RequiresPermissions("hhx:hhxInvest:view")
	@RequestMapping(value = "form")
	public String form(HhxInvest hhxInvest, Model model) {
		model.addAttribute("hhxInvest", hhxInvest);
		return "modules/hhx/hhxInvestForm";
	}

	@RequiresPermissions("hhx:hhxInvest:edit")
	@RequestMapping(value = "save")
	public String save(HhxInvest hhxInvest, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hhxInvest)){
			return form(hhxInvest, model);
		}
		hhxInvestService.save(hhxInvest);
		addMessage(redirectAttributes, "保存融资成功");
		return "redirect:"+Global.getAdminPath()+"/hhx/hhxInvest/?repage";
	}
	
	@RequiresPermissions("hhx:hhxInvest:edit")
	@RequestMapping(value = "delete")
	public String delete(HhxInvest hhxInvest, RedirectAttributes redirectAttributes) {
		hhxInvestService.delete(hhxInvest);
		addMessage(redirectAttributes, "删除融资成功");
		return "redirect:"+Global.getAdminPath()+"/hhx/hhxInvest/?repage";
	}
	
	@RequestMapping(value= {"wxsave",""})
	public String wxsave(HttpServletRequest request, HttpServletResponse response, String userid,String category,String description,String amt,String tel,String company,String contactname) {
		String id = UUID.randomUUID().toString().replace("-", "");
		int result = hhxInvestService.insertInvest(id,userid,category,description,amt,tel,company,contactname);
		Map<String, String> map = new HashMap<String, String>();
		if (result>0) {
			map.put("result","true");
			map.put("msg","保存成功");
		}else {
			map.put("result","false");
			map.put("msg","保存失败");
		}
		return renderString(response, map);
	}
	
	@RequestMapping(value = {"wxlist", ""})
	public String wxlist(HhxInvest hhxInvest, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HhxInvest> page = hhxInvestService.findPage(new Page<HhxInvest>(request, response), hhxInvest); 
		model.addAttribute("page", page);
		return renderString(response, page);
	}
	
	@RequestMapping(value = {"wxget", ""})
	public String wxget(HttpServletRequest request, HttpServletResponse response, @RequestParam(required=false) String id) {
		HhxInvest entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hhxInvestService.get(id);
		}
		if (entity == null){
			entity = new HhxInvest();
		}
		return renderString(response, entity);
	}

}